﻿using System;
using Ayatta.Contract;
using Ayatta.Contract.Domain;

namespace Web.Core.OrderHandler
{
    /// <summary>
    /// 处理虚拟拍币订单，用户支付成功后将等值的拍币发放给用户
    /// </summary>
    internal sealed class BidCurrencyHandler : IAutoHandler
    {
        private readonly int itemId;
        private readonly IStoreService storeService;
        private readonly IPaymentService paymentService;
        public BidCurrencyHandler(int itemId)
        {
            this.itemId = itemId;
            storeService = Application.Container.GetInstance<IStoreService>();
            paymentService = Application.Container.GetInstance<IPaymentService>();
        }

        public void Process(OrderItem item)
        {
            if (item.ItemId == itemId && item.Status==OrderStatus.Pending)
            {
                string message;
                var time = DateTime.Now;
                var extra = "竞拍获得";
                var sku = storeService.GetSku(item.SkuId);
                var status = paymentService.BidCurrencyIncrease(item.BuyerId, item.CreatedBy,(int) sku.Price, true, 2,item.OrderId, extra, out message);
                if (status)
                {
                    item.Status = OrderStatus.Finished;
                    item.ShippingType = ShippingType.Virtual;
                    item.Extra = "系统已自动为您发放" +(int)sku.Price + "免费拍币，请注意查收！" + time.ToString("yyyy-MM-dd HH:mm:ss");
                }
                else
                {
                    Application.Logger.Warn("BidCurrencyHandler：" + message);
                }
            }
        }
    }
}